﻿using System;
using System.Data;
using System.Windows.Forms;
using EP.Base.BaseService.Manager.Container;
using EP.Base.BaseService.Manager.Interface.Data;
using EP.Base.BaseService.Manager.Model.Entity;

namespace EP.Base.BaseService.Manager.Tools.InitDataFromExcel
{
    public partial class Form1 : Form
    {
        private IIndexConfigs _indexConfigs = AppContainer.Resolve<IIndexConfigs>();
        private ILogstashConfigLogs _logstashConfigLogs = AppContainer.Resolve<ILogstashConfigLogs>();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //初始化一个OpenFileDialog类
            OpenFileDialog fileDialog = new OpenFileDialog();

            //判断用户是否正确的选择了文件
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择文件的后缀名
               
                textBox1.Text = fileDialog.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var dataTable = ExcelHelper.InputFromExcel(textBox1.Text, textBox5.Text);

            foreach (DataRow dr in dataTable.Rows)
            {
                
                var 索引 = DObjToString(dr["索引"]);
                if(string.IsNullOrWhiteSpace(索引))
                    continue;
                

                var 别名 = DObjToString(dr["别名"]);
                var 后缀 = DObjToString(dr["后缀"]);
                var 说明 = DObjToString(dr["说明"]);
                var 计划 = DObjToString(dr["计划"]);
                var 存储方式 = DObjToString(dr["存储方式"]);
                var 输入 = DObjToString(dr["输入"]);
                var 输出 = DObjToString(dr["输出"]);
                var 服务器 = DObjToString(dr["服务器"]);
                var 是否备份 = DObjToString(dr["是否备份"]);
                var 保留数据范围 = DObjToString(dr["保留数据范围"]);
                var 联系人 = DObjToString(dr["联系人"]);
                var 状态 = DObjToString(dr["状态"]);
                var 配置名称 = DObjToString(dr["配置名称"]);
                var indexConfig = new IndexConfig
                {
                    ClusterId = int.Parse(textBox3.Text),
                    IndexNamePrefix = 索引.Split('-')[0],
                    Explain = 说明,
                    Alias=别名,
                    Users=联系人,
                    Source=textBox4.Text,
                    CreateTime = DateTime.Now,
                    NeedBackup = 是否备份=="是",
                    RetainTimeRange = 保留数据范围,
                    UseState = 状态,
                    IndexNameSuffix=后缀
                };
                
                if (存储方式 == "按天存储")
                    indexConfig.StorageMode = 4;
                else if (存储方式 == "按月存储")
                    indexConfig.StorageMode = 3;

                else if (存储方式 == "按年存储")
                    indexConfig.StorageMode = 2;
                else if (存储方式 == "存储全部")
                {
                    if (textBox5.Text == "logstash拉取")
                        indexConfig.StorageMode = 0;
                    else if (textBox5.Text == "应用程序写入")
                        indexConfig.StorageMode = 1;
                }

                if (!checkBox2.Checked)
                {
                    Log(索引);
                    int indexConfigId = _indexConfigs.Add(indexConfig);

                    if (checkBox1.Checked)
                    {
                        _logstashConfigLogs.Add(new LogstashConfigLog
                        {
                            IndexConfig = indexConfigId,
                            CreateTime = DateTime.Now,
                            InputType = 输入,
                            OutputType = 输出,
                            Schedule = 计划,
                            Server = 服务器,
                            UseState = 状态,
                            ConfFileName = 配置名称
                        });
                    }
                }
                else
                {
                    //生成语句
                    string sql = string.Format(@"INSERT INTO [IndexConfig]
			([ClusterId],[IndexNamePrefix],[IndexNameSuffix],[Alias],[Explain],[StorageMode],[Users],[Source],[NeedBackup],[RetainTimeRange],[UseState],[CreateTime])
			VALUES
			({0},'{1}','{2}','{3}','{4}',{5},'{6}','{7}',{8},'{9}','{10}',GETDATE())"
                        , indexConfig.ClusterId
                        , indexConfig.IndexNamePrefix
                        , indexConfig.IndexNameSuffix
                        , indexConfig.Alias
                        , indexConfig.Explain
                        , indexConfig.StorageMode
                        , indexConfig.Users
                        , indexConfig.Source
                        , indexConfig.NeedBackup?1:0
                        , indexConfig.RetainTimeRange
                        , indexConfig.UseState);
                    Log(sql,false);

                    Log("-----------------------------------------------------------------------------", false);
                    string sql1 = string.Format(@"INSERT INTO [LogstashConfigLog]
			([IndexConfig],[InputType],[OutputType],[Schedule],[Server],[UseState],[ConfFileName],[CreateTime])
			VALUES
			(@IndexConfig,'{0}','{1}','{2}','{3}','{4}','{5}',GETDATE()) "
                        , 输入
                        , 输出
                        , 计划
                        , 服务器
                        , 状态
                        , 配置名称);
                    Log(sql1, false);
                }

            }

        }
        private static string DObjToString( object value)
        {
            if (value == null)
                return string.Empty;
            else
            {
                return value.ToString();
            }
        }
        

        private void Log(string msg,bool addTime=true)
        {
            textBox2.AppendText(addTime?DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + msg:msg);
            textBox2.AppendText("\r\n");
        }
        private void textBox2_TextChanged_1(object sender, EventArgs e)
        {

            this.textBox2.SelectionStart = this.textBox2.Text.Length;
            this.textBox2.SelectionLength = 0;
            this.textBox2.ScrollToCaret();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
